/*     */ package com.zimbra.cs.db;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.datasource.DataSourceManager;
/*     */ import com.zimbra.cs.datasource.imap.ImapFolder;
/*     */ import com.zimbra.cs.datasource.imap.ImapFolderCollection;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import java.sql.PreparedStatement;
/*     */ import java.sql.ResultSet;
/*     */ import java.sql.SQLException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DbImapFolder
/*     */ {
/*     */   static final String TABLE_IMAP_FOLDER = "imap_folder";
/*     */   
/*     */   public static ImapFolder getImapFolder(Mailbox mbox, DataSource ds, int itemId)
/*     */     throws ServiceException
/*     */   {
/*  40 */     DbPool.DbConnection conn = null;
/*  41 */     PreparedStatement stmt = null;
/*  42 */     ResultSet rs = null;
/*     */     try {
/*  44 */       conn = DbPool.getConnection(mbox);
/*  45 */       stmt = conn.prepareStatement("SELECT local_path, remote_path, uid_validity FROM " + getTableName(mbox) + " WHERE " + DbMailItem.IN_THIS_MAILBOX_AND + "item_id = ? ");
/*     */       
/*     */ 
/*     */ 
/*  49 */       int pos = DbMailItem.setMailboxId(stmt, mbox, 1);
/*  50 */       stmt.setInt(pos, itemId);
/*  51 */       rs = stmt.executeQuery();
/*  52 */       if (!rs.next())
/*  53 */         return null;
/*  54 */       String localPath = rs.getString("local_path");
/*  55 */       String remotePath = rs.getString("remote_path");
/*  56 */       Long uidValidity = Long.valueOf(rs.getLong("uid_validity"));
/*  57 */       if (rs.wasNull()) {
/*  58 */         uidValidity = null;
/*     */       }
/*  60 */       return new ImapFolder(ds, itemId, remotePath, localPath, uidValidity);
/*     */     } catch (SQLException e) {
/*  62 */       throw ServiceException.FAILURE("Unable to get IMAP folder data", e);
/*     */     } finally {
/*  64 */       DbPool.closeResults(rs);
/*  65 */       DbPool.closeStatement(stmt);
/*  66 */       DbPool.quietClose(conn);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static ImapFolderCollection getImapFolders(Mailbox mbox, DataSource ds)
/*     */     throws ServiceException
/*     */   {
/*  75 */     ImapFolderCollection imapFolders = new ImapFolderCollection();
/*     */     
/*  77 */     DbPool.DbConnection conn = null;
/*  78 */     PreparedStatement stmt = null;
/*  79 */     ResultSet rs = null;
/*     */     try {
/*  81 */       conn = DbPool.getConnection(mbox);
/*     */       
/*  83 */       stmt = conn.prepareStatement("SELECT item_id, local_path, remote_path, uid_validity FROM " + getTableName(mbox) + " WHERE " + DbMailItem.IN_THIS_MAILBOX_AND + "data_source_id = ?");
/*     */       
/*     */ 
/*     */ 
/*  87 */       int pos = 1;
/*  88 */       pos = DbMailItem.setMailboxId(stmt, mbox, pos);
/*  89 */       stmt.setString(pos++, ds.getId());
/*  90 */       rs = stmt.executeQuery();
/*     */       
/*  92 */       while (rs.next()) {
/*  93 */         int itemId = rs.getInt("item_id");
/*  94 */         String localPath = rs.getString("local_path");
/*  95 */         String remotePath = rs.getString("remote_path");
/*  96 */         Long uidValidity = Long.valueOf(rs.getLong("uid_validity"));
/*  97 */         if (rs.wasNull()) {
/*  98 */           uidValidity = null;
/*     */         }
/* 100 */         ImapFolder imapFolder = new ImapFolder(ds, itemId, remotePath, localPath, uidValidity);
/* 101 */         imapFolders.add(imapFolder);
/*     */       }
/*     */     } catch (SQLException e) {
/* 104 */       throw ServiceException.FAILURE("Unable to get IMAP folder data", e);
/*     */     } finally {
/* 106 */       DbPool.closeResults(rs);
/* 107 */       DbPool.closeStatement(stmt);
/* 108 */       DbPool.quietClose(conn);
/*     */     }
/*     */     
/* 111 */     ZimbraLog.datasource.debug("Found %d folders for %s", new Object[] { Integer.valueOf(imapFolders.size()), ds });
/* 112 */     return imapFolders;
/*     */   }
/*     */   
/*     */   public static ImapFolder createImapFolder(Mailbox mbox, DataSource ds, int itemId, String localPath, String remotePath, long uidValidity)
/*     */     throws ServiceException
/*     */   {
/* 118 */     DbPool.DbConnection conn = null;
/* 119 */     PreparedStatement stmt = null;
/*     */     try {
/* 121 */       conn = DbPool.getConnection(mbox);
/*     */       
/* 123 */       ZimbraLog.datasource.debug("createImapFolder: itemId = %d, localPath = %s, remotePath = %s, uidValidity = %d", new Object[] { Integer.valueOf(itemId), localPath, remotePath, Long.valueOf(uidValidity) });
/*     */       
/*     */ 
/* 126 */       stmt = conn.prepareStatement("INSERT INTO " + getTableName(mbox) + " (" + DbMailItem.MAILBOX_ID + "item_id, data_source_id, local_path, remote_path, uid_validity) " + "VALUES (" + DbMailItem.MAILBOX_ID_VALUE + "?, ?, ?, ?, ?)");
/*     */       
/*     */ 
/*     */ 
/* 130 */       int pos = 1;
/* 131 */       pos = DbMailItem.setMailboxId(stmt, mbox, pos);
/* 132 */       stmt.setInt(pos++, itemId);
/* 133 */       stmt.setString(pos++, ds.getId());
/* 134 */       stmt.setString(pos++, localPath);
/* 135 */       stmt.setString(pos++, remotePath);
/* 136 */       stmt.setLong(pos++, uidValidity);
/* 137 */       stmt.executeUpdate();
/* 138 */       conn.commit();
/* 139 */       return new ImapFolder(ds, itemId, remotePath, localPath, Long.valueOf(uidValidity));
/*     */     } catch (SQLException e) {
/* 141 */       throw ServiceException.FAILURE("Unable to store IMAP message data", e);
/*     */     } finally {
/* 143 */       DbPool.closeStatement(stmt);
/* 144 */       DbPool.quietClose(conn);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static void updateImapFolder(ImapFolder imapFolder)
/*     */     throws ServiceException
/*     */   {
/* 153 */     Mailbox mbox = DataSourceManager.getInstance().getMailbox(imapFolder.getDataSource());
/* 154 */     DbPool.DbConnection conn = null;
/* 155 */     PreparedStatement stmt = null;
/*     */     try {
/* 157 */       conn = DbPool.getConnection(mbox);
/*     */       
/* 159 */       stmt = conn.prepareStatement("UPDATE " + getTableName(mbox) + " SET local_path = ?, remote_path = ?, uid_validity = ?" + " WHERE " + DbMailItem.IN_THIS_MAILBOX_AND + "data_source_id = ? AND item_id = ?");
/*     */       
/*     */ 
/*     */ 
/* 163 */       int pos = 1;
/* 164 */       stmt.setString(pos++, imapFolder.getLocalPath());
/* 165 */       stmt.setString(pos++, imapFolder.getRemoteId());
/* 166 */       stmt.setLong(pos++, imapFolder.getUidValidity());
/* 167 */       pos = DbMailItem.setMailboxId(stmt, mbox, pos);
/* 168 */       stmt.setString(pos++, imapFolder.getDataSource().getId());
/* 169 */       stmt.setInt(pos++, imapFolder.getItemId());
/* 170 */       int numRows = stmt.executeUpdate();
/* 171 */       if (numRows != 1) {
/* 172 */         throw ServiceException.FAILURE(String.format("Incorrect number of rows updated (%d) for %s", new Object[] { Integer.valueOf(numRows), imapFolder }), null);
/*     */       }
/*     */       
/*     */ 
/* 176 */       conn.commit();
/*     */     } catch (SQLException e) {
/* 178 */       throw ServiceException.FAILURE("Unable to update " + imapFolder, e);
/*     */     } finally {
/* 180 */       DbPool.closeStatement(stmt);
/* 181 */       DbPool.quietClose(conn);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static void deleteImapData(Mailbox mbox, String dataSourceId)
/*     */     throws ServiceException
/*     */   {
/* 190 */     ZimbraLog.datasource.info("Deleting IMAP data for DataSource %s", new Object[] { dataSourceId });
/*     */     
/* 192 */     if (StringUtil.isNullOrEmpty(dataSourceId)) {
/* 193 */       return;
/*     */     }
/* 195 */     DbPool.DbConnection conn = null;
/* 196 */     PreparedStatement stmt = null;
/*     */     
/*     */     try
/*     */     {
/* 200 */       conn = DbPool.getConnection(mbox);
/*     */       
/* 202 */       stmt = conn.prepareStatement("DELETE FROM " + getTableName(mbox) + " WHERE " + DbMailItem.IN_THIS_MAILBOX_AND + "data_source_id = ?");
/*     */       
/*     */ 
/* 205 */       int pos = 1;
/* 206 */       pos = DbMailItem.setMailboxId(stmt, mbox, pos);
/* 207 */       stmt.setString(pos++, dataSourceId);
/* 208 */       stmt.executeUpdate();
/* 209 */       conn.commit();
/*     */     } catch (SQLException e) {
/* 211 */       throw ServiceException.FAILURE("Unable to delete IMAP data", e);
/*     */     } finally {
/* 213 */       DbPool.closeStatement(stmt);
/* 214 */       DbPool.quietClose(conn);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static void deleteImapFolder(Mailbox mbox, DataSource ds, ImapFolder folder)
/*     */     throws ServiceException
/*     */   {
/* 223 */     ZimbraLog.datasource.info("Deleting IMAP data for %s in %s", new Object[] { folder, ds });
/*     */     
/* 225 */     DbPool.DbConnection conn = null;
/* 226 */     PreparedStatement stmt = null;
/*     */     
/*     */     try
/*     */     {
/* 230 */       conn = DbPool.getConnection(mbox);
/*     */       
/* 232 */       stmt = conn.prepareStatement("DELETE FROM " + getTableName(mbox) + " WHERE " + DbMailItem.IN_THIS_MAILBOX_AND + "data_source_id = ? and item_id = ?");
/*     */       
/*     */ 
/* 235 */       int pos = 1;
/* 236 */       pos = DbMailItem.setMailboxId(stmt, mbox, pos);
/* 237 */       stmt.setString(pos++, ds.getId());
/* 238 */       stmt.setInt(pos++, folder.getItemId());
/* 239 */       stmt.executeUpdate();
/* 240 */       conn.commit();
/*     */     } catch (SQLException e) {
/* 242 */       throw ServiceException.FAILURE("Unable to delete IMAP folder", e);
/*     */     } finally {
/* 244 */       DbPool.closeStatement(stmt);
/* 245 */       DbPool.quietClose(conn);
/*     */     }
/*     */   }
/*     */   
/*     */   public static String getTableName(int mailboxId, int groupId) {
/* 250 */     return DbMailbox.qualifyTableName(groupId, "imap_folder");
/*     */   }
/*     */   
/*     */   public static String getTableName(Mailbox mbox) {
/* 254 */     return DbMailbox.qualifyTableName(mbox, "imap_folder");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/db/DbImapFolder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */